package com.dmcloud.dm.auth.service;

import cn.hutool.core.util.ArrayUtil;
import com.dmcloud.common.constant.CommonConstants;
import com.dmcloud.common.constant.SecurityConstants;
import com.dmcloud.common.util.reponse.ResultMessage;
import com.dmcloud.system.dto.UserInfo;
import com.dmcloud.system.entity.SysUser;
import com.dmcloud.system.feign.IUserClient;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by xiaodao
 * date: 2019/11/2
 */
@Slf4j
@Service
@AllArgsConstructor
public class DmUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private IUserClient iUserClient;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        ResultMessage<UserInfo> userInfo = iUserClient.getUserInfo(username);
        UserDetails userDetails = getUserDetails(userInfo);
        return userDetails;
    }

    private UserDetails getUserDetails(ResultMessage<UserInfo> userInfo) {
        if (userInfo == null || userInfo.getData() == null) {
            throw new UsernameNotFoundException("用户不存在");
        } else {

            UserInfo data = userInfo.getData();

            Set<String> authSet = new HashSet<>();
            if (ArrayUtil.isNotEmpty(data.getRoles())) {
                //获取角色
                Arrays.stream(data.getRoles()).forEach(r -> authSet.add(SecurityConstants.ROLE + r));
                //获取资源
                if (ArrayUtil.isNotEmpty(data.getPermissions())) {
                    authSet.addAll(Arrays.asList(data.getPermissions()));
                }
            }
            Collection<? extends GrantedAuthority> authorities
                    = AuthorityUtils.createAuthorityList(authSet.toArray(new String[0]));
            SysUser user = data.getSysUser();
            // 构造security用户
            boolean locked = user.getAccountNonLocked().equals(CommonConstants.STATUS_NORMAL);
//            return new DmUserDetails(user.getId(), user.getDeptId(), user.getAccount(), SecurityConstants.BCRYPT + user.getPassword(),
////                    locked, true, true, true, authorities);
            return new DmUserDetails(user.getName(), SecurityConstants.BCRYPT + user.getPassword(),
                    true,true,true
                    ,locked,
                    authorities,user.getId(),
                    user.getDeptId(),
                    user.getTenantId(),
                    user.getAccount(),
                    user.getName(),
                    user.getSex());


        }
    }
}
